$include("$theme/login.css");


with ($ns("fp"))
{
    fp.AppLoginClass = function()
    {
        var me = this;
        
        me.init = function()
        {
            document.title = $msg("fp", "LOGIN_TITLE");
            $translate(document.body);

            $("form").submit(_onsubmit);
            $("#loginPanel").fadeIn(function(){
                $("#txtUserName").focus();
            });
        };
        
        
        
        function _onsubmit(e)
        {
            e.preventDefault();
            if ($isEmpty($("#txtUserName").val()) || $isEmpty($("#txtPassword").val()))
            {
                return;
            }
            
            $("#error").text("");
            $("input").attr("disabled", true);
            $("#btnLogin").text("").attr("disabled", true);
            
            g.Account.login({
                email: $("#txtUserName").val(),
                password: $("#txtPassword").val(),
                success: function(e)
                {
                    $("input").attr("disabled", false);
                    $("#btnLogin").text($msg("fp", "SIGN_IN")).attr("disabled", false);
                    
                    fp.Preferences.set("userName", $("#txtUserName").val());
                    fp.Preferences.set("password", $("#txtPassword").val());
                    
                    location.href = "Main.html";
                },
                error: function(e)
                {
                    $("input").attr("disabled", false).get(1).select();
                    $("#btnLogin").text($msg("fp", "SIGN_IN")).attr("disabled", false);
                    $("#error").text($msg("fp", "SIGN_IN_FAIL"));
                }
            });
        }
        
        return me;
    };
}